const {Controller} = require('egg');

const createRule = {
    username: 'string', 
    password:{
        type:'string',
        required:true,
        min:4,
        max:8
    }
};

class UserController extends Controller{
    //注册
    async registry(){
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;
        
        try{
            //校验
            ctx.validate(createRule)
            //用户是否存在
            let userData = await service.user.selectUser(username);

            if(userData.length){
                //此人存在
                ctx.body = {
                    code:2,
                    msg:'此人已存在'
                }
            }else{
                let age = null;
                //不存在
                try{
                    await service.user.registry(username,password,age);
                    ctx.body = {
                        code:1,
                        msg:'注册成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }
    //登录
    async login(){
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;
        try{
            ctx.validate(createRule);
            let loginData = await service.user.login(username,password);
            if(loginData.length){
                ctx.body = {
                    code:1,
                    msg:'登录成功'
                }
            }else{
                ctx.body = {
                    code:0,
                    msg:'登录失败'
                }
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }
    //注销
    async logout(){
        let {ctx,service} = this;
        let {id} = ctx.query;
        if(!id){
            return ctx.body = {
                code:2,
                msg:'缺失参数'
            }
        }

        try{
            
            await service.user.logout(id);
            ctx.body = {
                code:1,
                msg:'注销成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }
    //修改密码
    async editpassword(){
        let {ctx,service} = this;
        let {id,password,newPassword} = ctx.request.body;
        if(!id || !password || !newPassword){
            return ctx.body = {
                code:2,
                msg:'参数缺失'
            }
        }

        //userid  原密码  新密码
        let pwdSome = await service.user.pwdSome(id,password);

        if(pwdSome.length){
            //说明密码一致
            try{
                await service.user.editPassword(newPassword,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:0,
                msg:'原密码不正确'
            }
        }
    }
    //修改个人信息
    async editInfo(){
        let {ctx,service} = this;
        let {username,age,id} = ctx.request.body;
        if(!username || !id){
            return ctx.body = {
                code:2,
                msg:'参数缺失'
            }
        }

        try{
            await service.user.editInfo(username,age,id);
            ctx.body = {
                code:1,
                msg:'修改成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }
}

module.exports = UserController